﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eSpaceLib;

namespace com.huawei.ebg.esdk.ucsdk.client.control
{

    public partial class eSpaceStatus : Button
    {
        public eSpaceStatus()
        {
            InitializeComponent();
            SetEventListener();
        }
        //~eSpaceStatus()
        //{
        //    if (m_registered)
        //    {
        //        //Type为0表示订阅。Type为1表示取消订阅
        //        CtrlBusiness.SubScribe(_ucAccount, 1);
        //    }
        //    UnsetEventListener();
        //}

        //注册监听事件
        public void SetEventListener()
        {
            if (null == myListener)
            {
                myListener = new IECSExternEventHandler(this);
            }

            myListener.RegistEventListener();
        }

        //撤销监听事件
        public void UnsetEventListener()
        {
            if (null != myListener)
            {
                myListener.UnregistEventListener();

            }

        }

        # region 自定义字段

        private DateTime clickTime;
        private bool isClicked = false;

        private bool m_subscribed;
        private IECSExternEventHandler myListener;//事件监听器
        private string _ucAccount;
        [Description("被叫UC帐号")]
        public string UCAccount
        {
            get { return _ucAccount; }
            set { _ucAccount = value;
            FetchStatus();
            }
        }
        private int _status;
        [Description("控件绑定的UC帐号对应的状态值")]
        public int  Status
        {
            get { return _status; }
            set { _status = value;
            RefreshStatus();
            }
        }

        private Image _imgOnline = Properties.Resources.Online;
        [Description("Online状态图片")]
        public Image IMGOnline
        {
            get { return _imgOnline; }
            set { _imgOnline = value; }
        }

        private Image _imgBusy = Properties.Resources.Busy;
        [Description("Busy状态图片")]
        public Image IMGBusy
        {
            get { return _imgBusy; }
            set { _imgBusy = value; }
        }

        private Image _imgCalling = Properties.Resources.Calling;
        [Description("Calling状态图片")]
        public Image IMGCalling
        {
            get { return _imgCalling; }
            set { _imgCalling = value; }
        }

        private Image _imgLeave = Properties.Resources.Leave;
        [Description("Leave状态图片")]
        public Image IMGLeave
        {
            get { return _imgLeave; }
            set { _imgLeave = value; }
        }

        private Image _imgOffline = Properties.Resources.Offline;
        [Description("Offline状态图片")]
        public Image IMGOffline
        {
            get { return _imgOffline; }
            set { _imgOffline = value; }
        }

        private Image _imgNoDisturb = Properties.Resources.NoDisturb;
        [Description("NoDisturb状态图片")]
        public Image IMGNoDisturb
        {
            get { return _imgNoDisturb; }
            set { _imgNoDisturb = value; }
        }

        private Image _imgError = Properties.Resources.Error;
        [Description("获取状态异常图片")]
        public Image IMGError
        {
            get { return _imgError; }
            set { _imgError = value; }
        }

        private string _txtIMChat = "发送即时消息";
        [Description("下拉框发送即时消息文本")]
        public string TextIMChat
        {
            get { return _txtIMChat; }
            set { _txtIMChat = value; }
        }

        private string _txtDialCall = "发起呼叫";
        [Description("下拉框发起呼叫文本")]
        public string TextDialCall
        {
            get { return _txtDialCall; }
            set { _txtDialCall = value; }
        }

        private Image _imgIMChat = Properties.Resources.IMChat;
        [Description("下拉框发送即时消息图片")]
        public Image IMGIMChat
        {
            get { return _imgIMChat; }
            set { _imgIMChat = value; }
        }

        private Image _imgDialCall = Properties.Resources.DialCall;
        [Description("下拉框发起呼叫图片")]
        public Image IMGDialCall
        {
            get { return _imgDialCall; }
            set { _imgDialCall = value; }
        }

        # endregion

        //定义双击事件
        public new event EventHandler Click;
        public new event EventHandler DoubleClick;
        //上次点击时间的变量，初始值是系统定义的双击时间间隔

        protected override void OnClick(EventArgs e)
        {
            //重写OnClick事件，重构Double触发机制
            //now是系统运行到目前的时间
            //int now = Environment.TickCount;

            base.OnClick(e);
            if (this.Click != null)
            {
                Click(this, e);
            }
            else
            {
                try
                {
                    ToolStripDropDown dropDown = new ToolStripDropDown();
                    //即时消息下拉按键设置
                    ToolStripButton tsButton = new ToolStripButton();
                    tsButton.Text = _txtIMChat;
                    tsButton.Image = _imgIMChat;
                    tsButton.Tag = "IMChat";
                    tsButton.Click += new EventHandler(tsButton_Click);
                    dropDown.Items.Add(tsButton);
                    //直接呼叫下拉按键设置
                    tsButton = new ToolStripButton();
                    tsButton.Text = _txtDialCall;
                    tsButton.Image = _imgDialCall;
                    tsButton.Tag = "DialCall";
                    tsButton.Click += new EventHandler(tsButton_Click);
                    dropDown.Items.Add(tsButton);

                    //设置ToolStripDropDown弹出位置
                    Point location = Utilities.GetClientLocation(this, dropDown);
                    dropDown.Show(this, location, ToolStripDropDownDirection.BelowRight);
                }
                catch (Exception ex)
                {
                    //throw ex;
                }
            }
            //Console.WriteLine(DateTime.Now.Millisecond);
            TimeSpan span = DateTime.Now - clickTime;
            if (span.TotalMilliseconds < SystemInformation.DoubleClickTime)
            {
                if (isClicked)
                {
                    isClicked = false;
                    if (DoubleClick != null)
                    {
                        DoubleClick(this, e);
                    }
                    return;
                }
             
            }
            else
            {
                isClicked = true;
                clickTime = DateTime.Now;
            }

            //两次点击的时间间隔低于系统定义的点击间隔，则触发双击事件
            //if (now - previousClick <= SystemInformation.DoubleClickTime)
            //{
            //    if (DoubleClick != null)
            //    {
            //        DoubleClick(this, EventArgs.Empty);
            //        previousClick = now;
            //        return;
            //    }
            //}

            
        }

        private void tsButton_Click(object sender, EventArgs e)
        {
            try
            {
                switch (((string)((ToolStripButton)sender).Tag))
                {
                    case "IMChat":
                        CtrlBusiness.ShowIMChat(_ucAccount);
                        break;
                    case "DialCall":
                        CtrlBusiness.DirectCall(_ucAccount, string.Empty);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
        public void FetchStatus()
        {
            try
            {
                if (null != this._ucAccount && this._ucAccount.Length > 0)
                {
                    this._status = CtrlBusiness.GetStatus(_ucAccount);

                    //
                    CtrlBusiness.SubScribe(_ucAccount, 0);
                    this.m_subscribed = true;

                    RefreshStatus();
                }
            }
            catch (Exception ex)
            {
                this._status = 0;
                //throw ex;
            }
            
        }


        public void RefreshStatus()
        {
                //int myStatus = CtrlBusiness.GetStatus(_ucAccount);
                switch (this._status)
                {
                    case 0:
                        this.Image = _imgOffline;
                        break;
                    case 1:
                        this.Image = _imgOnline;
                        break;
                    case 2:
                        this.Image = _imgOffline;
                        break;
                    case 3:
                        this.Image = _imgBusy;
                        break;
                    case 4:
                        this.Image = _imgLeave;
                        break;
                    case 5:
                        this.Image = _imgNoDisturb;
                        break;
                    case 6:
                        this.Image = _imgError;
                        break;
                    case 10006:
                        this.Image = _imgCalling;
                        break;
                    default:
                        break;
                }

        }
    }
}
